<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="系统模块" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入系统模块"
          clearable
          style="width: 240px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人员" prop="createMan">
        <el-input
          v-model="queryParams.createMan"
          placeholder="请输入操作人员"
          clearable
          style="width: 240px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="exceptionCode">
        <el-select
          v-model="queryParams.exceptionCode"
          placeholder="操作状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="操作时间">
              <el-date-picker
                v-model="queryParams.dateRange"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"

        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleClean"

        >清空
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <a href="http://localhost:8081/log/exportLog" target="_blank">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
          >导出
          </el-button>
        </a>
      </el-col>
    </el-row>

    <el-table ref="tables" v-loading="loading" :data="list" @selection-change="handleSelectionChange"
              :default-sort="{prop: 'date', order: 'descending'}">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="日志编号" align="center" prop="id"/>
      <el-table-column label="系统模块" align="center" prop="description"/>
      <el-table-column label="操作类型" align="center" prop="type"/>
      <el-table-column label="操作人员" align="center" prop="createMan" width="100" :show-overflow-tooltip="true"/>
      <el-table-column label="操作状态" align="center" prop="exceptionCode">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.exceptionCode === '2000'">成功</el-tag>
          <el-tag v-else type="danger">失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作日期" align="center" prop="createDate"
                       sortable width="180">

      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row,scope.index)"
          >详细
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      :current-page.sync="page.currentPage"
      :total="total"
      @current-change="getList()"
      layout="prev, pager, next, jumper">
    </el-pagination>

    <!-- 操作日志详细 -->
    <el-dialog title="操作日志详细" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作模块：">{{ form.description }} / {{ form.type }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登录信息：">{{ form.createMan }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作方法：">{{ form.method }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作信息：">{{ form.exceptionDetail }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="返回参数："><p style="width: 500px;word-wrap: break-word;">{{ form.result }}</p>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作状态：">
              <div v-if="form.exceptionCode === '2000'">正常</div>
              <div v-else>失败</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作时间：">{{ form.createDate }}</el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import axios from "../../api/TutorialDataApi";
import qs from "qs"

export default {
  name: "Log",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 100,
      // 表格数据
      list: [],
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 默认排序
      // defaultSort: {prop: 'operTime', order: 'descending'},
      // 表单参数
      form: {},

      options: [{
        value: 2000,
        label: '成功'
      }, {
        value: 4000,
        label: '失败'
      }],

      // 查询参数
      queryParams: {

        description: undefined,
        createMan: undefined,
        exceptionCode: undefined,
        dateRange: []
      },
      page: {
        currentPage: 1,
        pageSize: 10,
      }

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询登录日志 */
    getList() {
      let p = {...this.queryParams, ...this.page}
      axios.get("http://localhost:8081/log/findAll", {params: p})
        .then(res => {
          this.list = res.data.data.records
          this.total = res.data.data.total
        })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.page.currentPage = 1;
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.page.currentPage = 1;
      this.queryParams = {}
      this.getList()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
      console.log(this.ids)
      console.log(this.multiple)
    },

    /** 详细按钮操作 */
    handleView(row) {
      this.open = true;
      this.form = row;
    },
    /** 删除按钮操作 */
    handleDelete() {
      this.$confirm('此操作将永久清空, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.post("http://localhost:8081/log/delete", this.ids)
          .then(res => {
            if (res.data.code === 2000) {
              this.$message.success("删除成功！")
              this.getList()
            } else {
              this.$message.error(res.data.msg)
            }
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$confirm('此操作将永久清空, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.get("http://localhost:8081/log/clean")
          .then(res => {
            if (res.data.code === 2000) {
              this.$message.success("删除成功！")
              this.getList()
            } else {
              this.$message.error(res.data.msg)
            }
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

  }
};
</script>

